package com.zh.api.controller.equipment;


import com.alibaba.fastjson.JSONObject;
import com.zh.common.base.BaseController;
import com.zh.common.utils.*;
import com.zh.equipment.domain.EquipmentDO;
import com.zh.equipment.service.EquipmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 设备绑定
 *
 * @author lds
 * @email 1992lcg@163.com
 * @date 2019-02-22 17:31:38
 */

@Controller
@RequestMapping("/equipment")
@Api("设备管理")
public class EquipmentController extends BaseController {
    @Autowired
    private EquipmentService equipmentService;


    @ResponseBody
    @RequestMapping(value = "list", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("获取设备绑定列表")
    public ResultVO list(@RequestBody Map<String, Object> params) {
        if (CheckEmptyUtil.isEmpty(params)) {
            return ResultVO.error(ResultEnum.PARAME_ERROR);
        }


        //查询列表数据
        Query query = new Query(params);
        List<EquipmentDO> equipmentList = equipmentService.list(query);
        int total = equipmentService.count(query);
        PageUtils pageUtils = new PageUtils(equipmentList, total);
        return ResultVO.success(pageUtils);
    }

    @ResponseBody
    @RequestMapping(value = "getEquipmentById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("获取设备绑定列表")
    ResultVO getEquipmentById(@RequestBody  EquipmentDO equipmentDO) {
        EquipmentDO equipment = equipmentService.get(equipmentDO.getId());
        return ResultVO.success(equipment);
    }

    /**
     * 保存
     */
    @ResponseBody
    @RequestMapping(value = "save", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("设备绑定添加")
    public ResultVO save(@RequestBody EquipmentDO equipment) {
        if(equipment==null || CheckEmptyUtil.isEmptys(equipment.getFamilyUserId()
        ,equipment.getEmName(),equipment.getEmNumber(),equipment.getEmType()
        ,equipment.getLocation())){

            return ResultVO.error(ResultEnum.PARAME_ERROR);
        }

        if (equipmentService.save(equipment) > 0) {
            return ResultVO.success();
        }
        return ResultVO.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping(value = "update", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("设备绑定修改")
    public ResultVO update(@RequestBody EquipmentDO equipment) {
        if(equipment==null || CheckEmptyUtil.isEmptys(equipment.getId(),equipment.getFamilyUserId()
                ,equipment.getEmName(),equipment.getEmNumber(),equipment.getEmType()
                ,equipment.getLocation())){

            return ResultVO.error(ResultEnum.PARAME_ERROR);
        }

        if(equipmentService.update(equipment)>0){
            return ResultVO.success();
        }
        return ResultVO.error();
    }

    /**
     * 删除
     */
    @RequestMapping(value = "remove", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    @ApiOperation("设备绑定删除")
    public ResultVO remove(@RequestBody EquipmentDO equipment) {
        if(equipment==null||CheckEmptyUtil.isEmpty(equipment.getId())){
            return ResultVO.error(ResultEnum.PARAME_ERROR);
        }

        if (equipmentService.remove(equipment.getId()) > 0) {
            return ResultVO.success();
        }
        return ResultVO.error();
    }

    /**
     * 删除
     */
    @RequestMapping(value = "batchRemove", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    @ApiOperation("批量删除设备绑定")
    public ResultVO batchRemove(@RequestBody JSONObject jsonParam) {
        if(jsonParam==null){
            return ResultVO.error(ResultEnum.PARAME_ERROR);
        }

        List<String> ids= (List<String>) jsonParam.get("ids");

        if(CheckEmptyUtil.isEmpty(ids)){
            return ResultVO.error(ResultEnum.PARAME_ERROR);
        }

        String[] strs1=ids.toArray(new String[ids.size()]);
       if(equipmentService.batchRemove(strs1)>0){
           return ResultVO.success();
       }
        return ResultVO.error();
    }


}
